﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
namespace PPFinance.Controls
{
    public class PPTabControl : System.Windows.Controls.TabControl
    {


        public PPTabControl()
        {

            this.SelectionChanged += new SelectionChangedEventHandler(PPTabControl_SelectionChanged);
            //this.Loaded += new RoutedEventHandler(CustomTabControl_Loaded);
        }

        void PPTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (dynamic oldItem in e.RemovedItems)
            {
                oldItem.Header = oldItem.HeaderText;
            }
            foreach (dynamic newItem in e.AddedItems)
            {
                if (newItem.CanClosed)
                    newItem.Header = newItem.HeaderControl;
                else
                    newItem.Header = newItem.HeaderText;
            }
        }

        //void CustomTabControl_Loaded(object sender, RoutedEventArgs e)
        //{
        //    
        //}



        /// <summary>
        /// 关闭其他
        /// </summary>
        /// <param name="butItem"></param>
        public void CloseAllBut(PPTabItem butItem)
        {
            var items = this.Items.Where(p => (p as PPTabItem).CanClosed && (p as PPTabItem) != butItem);
            if (items.Any())
            {
                CloseItem((items.Take(1).Single() as PPTabItem), butItem);
            }
        }

        private void CloseItem(PPTabItem item, PPTabItem butItem)
        {
            var items = this.Items.Where(p => (p as PPTabItem).CanClosed && (p as PPTabItem) != butItem);
            if (items.Any())
            {
                (items.Take(1).Single() as PPTabItem).Close();
                if (items.Any())
                {
                    CloseItem((items.Take(1).Single() as PPTabItem), butItem);
                }
            }

        }


        public void CloseGroup(PPTabItem pPTabItem)
        {

        }
    }
}
